package com.czl.base.data.source.impl.tengyun

import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.data.api.TengyunService
import com.czl.base.data.bean.PageParams
import com.czl.base.data.bean.WorkOrderPayBean
import com.czl.base.data.bean.tengyun.*
import com.czl.base.data.source.tengyun.TengyunDateSource
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import io.reactivex.Observable
import okhttp3.MediaType
import okhttp3.RequestBody

class TengyunDataImpl (private val tengyunService: TengyunService) : TengyunDateSource {

    private fun toGsonParams(params: Map<String, String>): RequestBody {
        var gson = Gson()
        var obj: String = gson.toJson(
            params
        )
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj)
    }

    private fun toGsonObj(params: HealthReportBean.Data): RequestBody {
        var gson = Gson()
        var obj: String = gson.toJson(
            params
        )
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj)
    }

    override fun getNoticeList(pageNo: Int, pageSize: Int, releaseType: Int, neighId: String): Observable<BaseBean<NoticeListBean>> {
        val json = JsonObject()
        json.addProperty("pageNo", pageNo)
        json.addProperty("pageSize", pageSize)
        json.addProperty("releaseType", releaseType)
        json.addProperty("neighId", neighId)
        var body: RequestBody =
            RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"),
                Gson().toJson(json)
            )
        return tengyunService.getNoticeList(body)
    }

    override fun updaReadingVolumeById(announcementId: Int): Observable<BaseBean<NoticeListBean>> {
        val json = JsonObject()
        json.addProperty("announcementId", announcementId)
        val body: RequestBody =
            RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"),
                Gson().toJson(json)
            )
        return tengyunService.updaReadingVolumeById(body)
    }

    override fun selectAnnouncementById(announcementId: Int): Observable<BaseBean<NoticeListBean.Data>> {
        val json = JsonObject()
        json.addProperty("announcementId", announcementId)
        val body: RequestBody =
            RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"),
                Gson().toJson(json)
            )
        return tengyunService.selectAnnouncementById(body)
    }

    override fun selectHealthyAll(
        pageNo: Int,
        pageSize: Int,
        userId: Int,
        neighId: Int
    ): Observable<BaseBean<HealthReportBean>> {
        val json = JsonObject()
        json.addProperty("pageNo", pageNo)
        json.addProperty("pageSize", pageSize)
        json.addProperty("userId", userId)
        json.addProperty("neighId", neighId)
        var body: RequestBody =
            RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"),
                Gson().toJson(json)
            )
        return tengyunService.selectHealthyAll(body)
    }

    override fun getAreaByUser(
        id: Int
    ): Observable<BaseBean<HealthUserBean.Data>> {
        val json = JsonObject()
        json.addProperty("Id", id)
        var body: RequestBody =
            RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"),
                Gson().toJson(json)
            )
        return tengyunService.getAreaByUser(body)
    }

    override fun selectHealthyAndRelativesById(
        healthyId: Int
    ): Observable<BaseBean<HealthReportBean.Data>> {
        val json = JsonObject()
        json.addProperty("healthyId", healthyId)
        var body: RequestBody =
            RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"),
                Gson().toJson(json)
            )
        return tengyunService.selectHealthyAndRelativesById(body)
    }

    override fun addHealthReport(params: HealthReportBean.Data): Observable<BaseBean<Any?>> {

        var body: RequestBody = toGsonObj(params)
        return tengyunService.addHealthReport(body)
    }

    override fun getBillByRoomIdList(
        tUserId: Int,
        payStatus: Int,
        roomIdList : List<String>
    ): Observable<BaseBean<HousesPaymentBean>> {
        val jsonObject = JsonObject()
        val array = JsonArray()
        roomIdList.forEach {
            array.add(it)
        }
        jsonObject.add("roomIdList", array)
        jsonObject.addProperty("payStatus", payStatus)
        jsonObject.addProperty("userId", tUserId)
        var body: RequestBody =
            RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"),
                Gson().toJson(jsonObject)
            )
        return tengyunService.getBillByRoomIdList(body)
    }

    override fun getDetailsByBillId(
        billId: Int
    ): Observable<BaseBean<PaymentData>> {
        val json = JsonObject()
        json.addProperty("billId", billId)
        var body: RequestBody =
            RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"),
                Gson().toJson(json)
            )
        return tengyunService.getDetailsByBillId(body)
    }

    override fun payChdBill(paramsJson: String): Observable<BaseBean<HouseOrderPayBean>> {
        val body : RequestBody = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            paramsJson
        )
        return tengyunService.payChdBill(body)
    }
}